<template>
  <div>
    <el-form ref="applyForm" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="领用需求" prop="requireSpare">
        <el-input
          v-model="formItem.requireSpare"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
        />
      </el-form-item>
      <el-form-item label="服务厅局" prop="officeId">
        <el-select v-model="formItem.officeId" placeholder="请选择服务厅局" @change="officeChange">
          <el-option v-for="(item) in offices" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="故障工单" prop="malfunction">
        <el-select v-model="formItem.malfunction" placeholder="请选择故障工单" @change="malfunctionChange">
          <el-option v-for="(item) in malfunctions" :key="item.id" :label="item.title" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-col :span="24">
        <div style="float:right">
          <el-button size="mini" type="warning" @click="ok('temporary')">保存</el-button>
          <el-button size="mini" type="primary" @click="ok('submit')">提交</el-button>
        </div>
      </el-col>
    </el-form>
  </div>
</template>
<script>
import { todoPage, donePage } from '@neu/dcam-ui/api/workOrder'
import { insititutions } from '@neu/dcam-ui/api/fixedAsset'
import { create, detail } from '@neu/dcam-ui/api/spareconsume'
const formItem = {
  // 工作流TaskId
  taskId: '',
  requireSpare: '',
  officeId: null,
  malfunction: null
}
export default {
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      offices: [],
      malfunctions: [],
      formItem: {
        ...formItem
      },
      rules: {
        requireSpare: [{ required: true, message: '领用需求不能为空', trigger: 'blur' }],
        officeId: [{ required: true, message: '服务厅局不能为空', trigger: 'change' }],
        malfunction: [{ required: true, message: '故障工单不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.fetch()
    const parameter = {
      s_type: 'malfunction',
      page: 0,
      size: 10000
    }
    todoPage(parameter).then(response => {
      this.malfunctions = response.data
      donePage(parameter).then(response => {
        this.malfunctions = this.mergeArray(this.malfunctions, response.data)
      })
    })
    insititutions().then(response => {
      const dcomInstitutionsArray = response
      this.dcomInstitutions = []
      if (dcomInstitutionsArray) {
        dcomInstitutionsArray.forEach((data, index) => {
          this.offices.push(data)
        })
      }
    })
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    mergeArray(arr1, arr2) {
      // eslint-disable-next-line no-array-constructor
      var _arr = new Array()
      for (var i = 0; i < arr1.length; i++) {
        _arr.push(arr1[i])
      }
      for (let i = 0; i < arr2.length; i++) {
        var flag = true
        for (var j = 0; j < arr1.length; j++) {
          if (arr2[i] === arr1[j]) {
            flag = false
            break
          }
        }
        if (flag) {
          _arr.push(arr2[i])
        }
      }
      return _arr
    },
    officeChange(value) {
      if (value) {
        const items = this.offices.filter(r => r.id === value)
        this.formItem.officeName = (items && items[0].name) || ''
      }
    },
    malfunctionChange(value) {
      if (value) {
        const items = this.malfunctions.filter(r => r.id === value)
        this.formItem.malfunctionName = (items && items[0].title) || ''
      }
    },
    ok(value) {
      this.$refs.applyForm.validate((valid) => {
        if (valid) {
          // 状态
          this.formItem.status = value
          this.formItem.taskId = this.taskId
          this.formItem.workNum = this.workOrderNum
          create(this.formItem).then(res => {
            if (res.status === 'success') {
              this.$emit('cancelNewOrderForm', 'refresh')
            }
          })
        }
      })
    }
  }
}
</script>
